package com.wbcs.system.component.application.report.configbean.editablereport;

import java.sql.SQLException;
import java.util.Map;

import com.wbcs.config.component.application.report.ColBean;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.exception.WbcsConfigLoadingException;
import com.wbcs.system.ReportRequest;
import com.wbcs.util.Consts;

public abstract class AbsEditActionBean
{
    protected EditActionGroupBean ownerGroupBean;

    public AbsEditActionBean(EditActionGroupBean ownerGroupBean)
    {
        this.ownerGroupBean=ownerGroupBean;
    }

    public EditActionGroupBean getOwnerGroupBean()
    {
        return ownerGroupBean;
    }

    public void setOwnerGroupBean(EditActionGroupBean ownerGroupBean)
    {
        this.ownerGroupBean=ownerGroupBean;
    }

    public abstract void parseActionscript(String reportTypeKey,String actionscript);

    public abstract void updateData(ReportRequest rrequest,ReportBean rbean,Map<String,String> mRowData,
            Map<String,String> mParamValues) throws SQLException;
    
    public EditableReportParamBean createParamBeanByColbean(String configColProperty,String reportKey,boolean enableHiddenCol,boolean isMust)
    {
        if(configColProperty==null||configColProperty.trim().equals("")) return null;
        String realColProperty=configColProperty.trim();
        if(realColProperty.endsWith("__old")) realColProperty=realColProperty.substring(0,realColProperty.length()-"__old".length());
        ReportBean rbean=this.ownerGroupBean.getOwnerUpdateBean().getOwner().getReportBean();
        ColBean cbean=rbean.getDbean().getColBeanByColProperty(realColProperty);
        if(cbean==null) throw new WbcsConfigLoadingException("加载报表"+rbean.getPath()+"失败，column/property为"+realColProperty+"的列不存在，无法为其创建更新参数对象");
        if(cbean.isNonValueCol()||cbean.isSequenceCol()||cbean.isControlCol())
        {
            if(!isMust) return null;
            throw new WbcsConfigLoadingException("加载报表"+cbean.getReportBean().getPath()+"失败，列"+cbean.getColumn()+"不是从数据库获取数据的列，不能做为更新列");
        }
        if(!isMust&&cbean.isNonFromDbCol()) return null;
        ColBean cbUpdateSrc=cbean;
        if(Consts.COL_DISPLAYTYPE_HIDDEN.equals(cbean.getDisplaytype()))
        {
            ColBean cbSrc=cbean.getUpdateColBeanSrc(false);
            if(cbSrc==null)
            {//没有被别的<col/>通过updatecol属性引用到
                if(!enableHiddenCol)
                {
                    if(!isMust) return null;
                    throw new WbcsConfigLoadingException("加载报表"+cbean.getReportBean().getPath()+"失败，列"+cbean.getColumn()+"为隐藏列，在这种情况不允许做为更新数据的字段");
                }
            }else
            {
                cbUpdateSrc=cbSrc;
            }
        }
        EditableReportParamBean paramBean=new EditableReportParamBean();
        EditableReportColBean ercbean=(EditableReportColBean)cbean.getExtendConfigDataForReportType(reportKey);
        if(ercbean!=null) paramBean.setDefaultvalue(ercbean.getDefaultvalue());
        paramBean.setOwner(cbean);
        this.ownerGroupBean.getOwnerUpdateBean().setParamBeanInfoOfColBean(cbUpdateSrc,paramBean,configColProperty,reportKey);
        return paramBean;
    }
    
    public void setRealParamnamesInDoPostLoadFinally()
    {
    }
    
    public Object clone(EditActionGroupBean newowner)
    {
        try
        {
            AbsEditActionBean newbean=(AbsEditActionBean)super.clone();
            newbean.ownerGroupBean=newowner;
            return newbean;
        }catch(CloneNotSupportedException e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
